/*************************************************************************
	> File Name: server.c
	> Author: luanlongsheng
	> Mail: luanlongshengv@163.com
	> Created Time: 2021年05月25日 星期二 19时00分45秒
 ************************************************************************/

#include "../common/color.h"
#include "../common/udp_server.h"
#include "../common/common.h"
#include "../common/head.h"
#include "../common/global.h"
#include "../common/game.h"
#include "../common/udp_epoll.h"

char *conf = "./server.conf";

struct User *rteam;
struct User *bteam;
int data_port;

//extern struct Map court;
int port = 0;

int main(int argc, char **argv) {
    int opt, listener, epoll_fd;
    pthread_t draw_t;
    //如果从终端指定端口，就是用终端输入窗口
    while ((opt = getopt(argc, argv, "p:")) != -1) { 
        switch (opt) {
            case 'p':
                port = atoi(optarg);
                break;
            default:
                fprintf(stderr, "Usage: %s [-p port]\n", argv[0]);
                exit(1);
        }
    }
    //更新传入值，参数个数减少，参数增多,optind：每读入一个值,optind就向后移动一位
    argc -= (optind - 1);
    argv += (optind - 1);
    //如果参数输多了会报错
    if (argc > 1) {
        fprintf(stderr, "Usage: %s [-p port]\n", argv[0]);
        exit(1);
    }
    //如果终端没有指定端口，则使用配置文件端口
    if (!port) port = atoi(get_value(conf, "PORT"));
    data_port = atoi(get_value(conf, "DATAPORT"));

    court.width = atoi(get_value(conf, "COLS"));
    court.height = atoi(get_value(conf, "LINES"));
    court.start.x = 1;
    court.start.y = 1;

    rteam = (struct User *)calloc(MAX, sizeof(struct User));
    bteam = (struct User *)calloc(MAX, sizeof(struct User));
    
    if ((listener = socket_create_udp(port)) < 0)  {
        perror("socket_create_udp");
        exit(1);
    }

    DBG(GREEN"INFO"NONE" : Server start on Port %d\n", port);
    
    //服务器端创建一个线程显示界面，一般不会这样做，在客户端更合理
    //pthread_create(&draw_t, NULL, draw, NULL);

    epoll_fd = epoll_create(MAX * 2);
    if (epoll_fd < 0) {
        perror("epoll_create");
        exit(1);
    }

    struct epoll_event ev, events[MAX * 2];

    ev.events = EPOLLIN;
    ev.data.fd = listener;

    epoll_ctl(epoll_fd, EPOLL_CTL_ADD, listener, &ev);

    usleep(1000);
    struct sockaddr_in client;
    socklen_t len = sizeof(client);
    
    while (1) {
        //w_gotoxy_puts(Message, 1, 1, "Waiting for login");
        DBG(YELLOW"EPOLL"NONE" : Before epoll_wait\n");
        int nfds = epoll_wait(epoll_fd, events, MAX * 2, -1);
        DBG(YELLOW"EPOLL"NONE" : After epoll_wait\n");
        
        for (int i = 0; i < nfds; i++) {
            if (events[i].data.fd == listener) {
               udp_accept(epoll_fd, listener); 
            }

            //char info[1024] = {0};
            //recvfrom(events[i].data.fd, (void *)info, sizeof(info), 0, (struct sockaddr *)&client, &len);
            //sprintf(info, "Login : %s : %d", inet_ntoa(client.sin_addr), ntohs(client.sin_port));
            //w_gotoxy_puts(Message, 1, 2, info);
        }

    }

    return 0;
}
